
package com.calendar.guos.client.maps;

import java.util.LinkedList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.InfoWindowContent.InfoWindowTab;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * In this example, we show a custom info window above each marker by listening
 * to the click event for each marker.
 */
public class MarkerInfoWindowDemo extends Composite{

  //52.131543, -106.634485
  public LatLng centerOfcampus = LatLng.newInstance(52.13255, -106.6349472);
  public int zoomLevel = 16;
  public LinkedList<MapArtifact> mapArtifactList = new LinkedList<MapArtifact>();
  public MapWidget map;

/*
  public static MapsDemoInfo init() {
    return new MapsDemoInfo() {
      @Override
      public MapsDemo createInstance() {
        return new MarkerInfoWindowDemo();
      }

      @Override
      public HTML getDescriptionHTML() {
        if (descHTML == null) {
          descHTML = new HTML(descString);
        }
        return descHTML;
      }

      @Override
      public String getName() {
        return "Display Info Windows Above Markers";
      }
    };
  }
*/

  public MarkerInfoWindowDemo() {

	mapArtifactList.add( new MapArtifact("Thorvaldson", 52.13209722, -106.6359222));
	mapArtifactList.add( new MapArtifact("Spinks", 52.13255, -106.6349472));

		
    map = new MapWidget(centerOfcampus, zoomLevel);
    //map.setSize("100%", "100%");
    onShow();
    //initWidget(map);
    //map.setUIToDefault();
  }


  public void onShow() {
	int widthSmallFlr = 200;
	int heightSmallFlr = 200;
    map.clearOverlays();

    // Add 10 markers to the map at random locations
//    LatLngBounds bounds = map.getBounds();
//    LatLng southWest = bounds.getSouthWest();
//    LatLng northEast = bounds.getNorthEast();
//    double lngSpan = northEast.getLongitude() - southWest.getLongitude();
//    double latSpan = northEast.getLatitude() - southWest.getLatitude();

	
	// Add some controls for the zoom level
	map.addControl(new LargeMapControl());

	if(mapArtifactList.size() > 0){
	    for(MapArtifact artifact: mapArtifactList) {
	    	
//	      LatLng point = LatLng.newInstance(southWest.getLatitude() + latSpan
//	          * Math.random(), southWest.getLongitude() + lngSpan * Math.random());
	    	
	      artifact.updateSchematic(heightSmallFlr, widthSmallFlr);
	      map.addOverlay(createMarker(artifact));
	    }
	}

  }

  private Marker createMarker(final MapArtifact artifact) {
    final Marker marker = new Marker(artifact.location);
    
    marker.addMarkerClickHandler(new MarkerClickHandler() {
      public void onClick(MarkerClickEvent event) {
  	    InfoWindowTab tabs[] = new InfoWindowTab[2];

  	    
        // An image that isn't loaded yet doesn't work well sometimes
        // Specify the width and height to work around this.

        
	    tabs[0] = new InfoWindowTab(artifact.name, "<h1>Tab 1 Content</h1>");
	    tabs[1] = new InfoWindowTab("Location", artifact.schematic);
	   
	      // An image that isn't loaded yet doesn't work well sometimes
	      // Specify the width and height to work around this.

        InfoWindow info = map.getInfoWindow();

        InfoWindowContent content = new InfoWindowContent(tabs, 1);
        
        //Add the floor plans to second tab
        displayInfoWindowMaxWidget(artifact, content);

        info.open(marker,
            content);
      }
    });
    
    return marker;
   
  }
  private void displayInfoWindowMaxWidget(final MapArtifact artifact, InfoWindowContent content) {
	    VerticalPanel panel = new VerticalPanel();
	    
	  	int heightMaxFlr = 400, widthMaxFlr= 400;
	 
	    content.setMaxTitle(new HTML("<i>Maximized Italic Boots</i>"));

	    

	    
	    artifact.updateSchematic(heightMaxFlr, widthMaxFlr);
	    panel.add(new HTML("<p>"+artifact.schematic+"<p>"));
	    
	    Button b = new Button("Enlarge Floor Map");
	    final Label l = new Label();
	    HorizontalPanel hp = new HorizontalPanel();
	    hp.add(b);
	    hp.add(l);
	    
	   // l.getElement().getStyle().setPropertyPx("margin", 7);
	    
	    b.addClickHandler(new ClickHandler() {
	      public void onClick(ClickEvent event) {
	        GWT.log("Got click in maximized window.", null);
	        if (l.getText().equals("")) {
	          l.setText("Hello World!");
	        } else {
	          l.setText("");
	        }
	      }
	    });
	    
	    panel.add(hp);
	    panel.setSpacing(10);
	    content.setMaxContent(panel);
	  
	  }
  private InfoWindowContent displayInfoWindowTabs() {

	    InfoWindowTab tabs[] = new InfoWindowTab[2];

	    tabs[0] = new InfoWindowTab("Tab 1", "<h1>Tab 1 Content</h1>");
	    tabs[1] = new InfoWindowTab("Tab 2", "<h1>Tab 2 Content</h1>");
	    final InfoWindowContent content = new InfoWindowContent(tabs, 1);
	    return content;
	  }
}